#include"SplitTool.h"
#include"Singleton.hpp"
#include"cppjieba/Jieba.hpp"
#include "Configuration.h"

SplitToolCN::SplitToolCN(Configuration *conf)
:m_Conf(conf){

}

SplitToolCN::~SplitToolCN(){

}

vector<string> SplitToolCN::cutCN(const string &str){
    std::vector<std::string>    words, words1, result;
    InforArgs                   inforArgs;
    // 最大概率法(MPSegment: MAX Probability) 分词所使用的词典路径
    const char * const DICT_PATH        = inforArgs.DICT_PATH.c_str();
    // 隐式马尔科夫模型(HMMSegment: Hidden Markov Model) 分词所使用的词典路径
    const char * const HMM_PATH         = inforArgs.HMM_PATH.c_str();
    // 用户自定义词典路径
    const char * const USER_DICT_PATH   = inforArgs.USER_DICT_PATH.c_str();
    // IDF路径
    const char * const IDF_PATH         = inforArgs.IDF_PATH.c_str();
    // 停用词路径
    const char * const STOP_WORD_PATH   = inforArgs.STOP_WORD_PATH.c_str();

    // 使用单例模式模板类, 来创建cppJieba::Jieba类
    cppjieba::Jieba             *pJieba = Singleton<cppjieba::Jieba>::getInstance(DICT_PATH, HMM_PATH, USER_DICT_PATH, IDF_PATH, STOP_WORD_PATH);
    // 对sentence句子进行分词
    pJieba->CutForSearch(str, words);

    // 中文停用词集
    std::set<std::string> stopWordList = m_Conf->getCN_StopWordList();
    // 进行停用词集去重
    DISTINCT_StopWord(words, words1, stopWordList);
    // 去除标点符号
    DISTINCT_punct(words1, result);
    
    return result;
}


void SplitToolCN::DISTINCT_StopWord(vector<string> &words,vector<string>&result,set<string>stopWordList){
    bool    isStopWord = false;
    for (auto it = words.begin(); it != words.end(); ++it) {
        for (auto stopWord = stopWordList.begin(); stopWord != stopWordList.end(); ++stopWord) {
            if (*it == *stopWord) {
                isStopWord = true;
                break;
            }
        }

        if (isStopWord) {
            isStopWord = false;
            continue;
        }

        result.emplace_back(*it);
    }
}
void SplitToolCN::DISTINCT_punct(vector<string>&words,vector<string>result){
    for (const auto & word : words) {
        if (std::ispunct(word[0]) || word == " ") {
            continue;
        }
        result.emplace_back(word);
    }
}
